package org.finesys.chat.core.base.embedding;


import org.finesys.chat.core.base.embedding.filter.Filter;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;

public interface EmbeddingStore<Embeded> {
    /**
     * 新增
     */
    String add(Embedding embedding);

    /**
     * 新增
     */
    String add(Embedding embedding, String key);

    /**
     * 新增
     */
    String add(Embedding embedding, Embeded embeded);

    List<String> addAll(List<Embedding> embeddings);

    List<String> addAll(List<Embedding> embeddings, List<Embeded> embededs);

    List<String> addAll(List<String> ids, List<Embedding> embeddings, List<Embeded> embededs);


    default void delete(String key) {
        if (StringUtils.hasText(key)) {
            deleteAll(Arrays.asList(key));
        }
    }

    default void deleteAll(List<String> keys) {
        throw new UnsupportedOperationException();
    }

    default void deleteAll(Filter filter) {
        throw new UnsupportedOperationException();
    }

    default void deleteAll() {
        throw new UnsupportedOperationException();
    }


    default EmbeddingSearchResult<Embeded> search(EmbeddingSearchRequest request) {
        throw new UnsupportedOperationException();
    }

    default List<EmbeddingMatch<Embeded>> findRelevant(Embedding embedding, int limit) {
        return this.findRelevant(embedding, limit, 0.0);
    }

    default List<EmbeddingMatch<Embeded>> findRelevant(Embedding embedding, int limit, double minScore) {
        EmbeddingSearchRequest.Builder builder = EmbeddingSearchRequest.builder();
        builder.queryEmbedding(embedding).limit(limit).minScore(minScore);
        EmbeddingSearchResult<Embeded> result = search(builder.build());
        return result.getMatches();
    }

    /**
     * 关闭方法
     */
    void stop();
}
